using System;
using System.Web;
using System.Web.Hosting;
using System.Configuration;
using System.Xml;
using System.Xml.Xsl;
using System.Text;
using System.IO;
using System.Globalization;

namespace SBPweb.Forge.Configuration
{
	/// <summary>
	/// Summary description for ConfigHandler
	/// </summary>
	public static class ConfigurationHandler
	{

		#region Private members

        private static VirtualPathProvider pathProvider = HostingEnvironment.VirtualPathProvider;
        private static VirtualDirectory rootDirectory;
		
		#endregion

		#region Property accessors

		/// <summary>
		/// Title of the application
		/// </summary>
		public static string ApplicationTitle
		{
			get
			{
				return ConfigurationManager.AppSettings["applicationTitle"];
			}
		}

		/// <summary>
		/// Version of the application
		/// </summary>
		public static System.Version ApplicationVersion
		{
			get
			{
				return new System.Version(ConfigurationManager.AppSettings["applicationVersion"]);
			}
		}

		/// <summary>
		/// Physycal root folder
		/// </summary>
		public static string PhysicalRootDirectory
		{
			get
			{
				return HostingEnvironment.ApplicationPhysicalPath;
			}
		}

		/// <summary>
		/// Virtual root folder
		/// </summary>
		public static VirtualDirectory VirtualRootDirectory
		{
			get
			{
				if (rootDirectory != null)
					return rootDirectory;

				return rootDirectory = pathProvider.GetDirectory("~/");
			}
		}

        #endregion

		#region Public helper methods

		/// <summary>
		/// Get section
		/// </summary>
		/// <typeparam name="T">type</typeparam>
		/// <returns>section</returns>
		public static T GetSection<T>() where T: ConfigurationSection
		{
			Type type = typeof(T);
			string name = type.Name;
			name = name.Substring(0, 1).ToLower(CultureInfo.CurrentCulture) + name.Substring(1);
			return GetSection<T>(name);
		}

		/// <summary>
		/// Get section
		/// </summary>
		/// <typeparam name="T">type</typeparam>
		/// <param name="sectionName">name of the section</param>
		/// <returns>section</returns>
		public static T GetSection<T>(string sectionName) where T : ConfigurationSection
		{
			return (T)ConfigurationManager.GetSection(sectionName);
		}

		/// <summary>
		/// Get section group
		/// </summary>
		/// <typeparam name="T">type</typeparam>
		/// <returns>section group</returns>
        public static T GetSectionGroup<T>() where T : ConfigurationSectionGroup
        {
            Type type = typeof(T);
            string name = type.Name;
            name = name.Substring(0, 1).ToLower(CultureInfo.CurrentCulture) + name.Substring(1);

            return GetSectionGroup<T>(name);
        }

		/// <summary>
		/// Get section group
		/// </summary>
		/// <typeparam name="T">type</typeparam>
		/// <param name="sectionName">name of the section</param>
		/// <returns>section group</returns>
        public static T GetSectionGroup<T>(string sectionName) where T : ConfigurationSectionGroup
        {
            System.Configuration.Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(VirtualRootDirectory.VirtualPath + "Web.Config");

            return (T)configuration.GetSectionGroup(sectionName);
        }

        public static VirtualDirectory GetRelativeVirtualDirectory(string relativePath)
        {
            return pathProvider.GetDirectory(string.Format("~/{0}", relativePath));
        }

		#endregion

	}
}